var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var fs = require("fs");
var device = require('express-device');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();




app.use(logger('dev'));
app.use(device.capture());
app.use(express.json());
// app.use(bodyParse.urlencoded({extended:false}));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));




app.all('*',function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

    if (req.method == 'OPTIONS') {

        res.send(200); /让options请求快速返回/
    }
    else {
        next();
    }
});

app.use('/', indexRouter);
app.use('/users', usersRouter);


app.get("/pjlist",function (req,res) {

    fs.readFile("./public/data.json",function (err,data) {

        if(!err){

           let result =   JSON.parse(data.toString());
           res.json(result);
          }

    });

});


app.get("/phone",function (req,res) {

    res.send("还没写移动端的....");
});

app.get("/bslist",function (req,res) {

    fs.readFile("./public/blogs/blog1.json",function (err,data) {

        if(!err){

            let result =   JSON.parse(data.toString());
            res.json(result);
        }

    });
});
app.listen(8040);

module.exports = app;
